package com.cabrida.livestar.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import android.widget.ImageView;

import com.caishuij.image.core.DisplayImageOptions;
import com.caishuij.image.core.ImageLoader;

/**
 */
public class MyUtils {

	private static ImageLoader imageLoader = ImageLoader.getInstance();

	private static DisplayImageOptions options = OptionImageUtil
			.getDelfaultOption();

	/**
	 * 加载图片
	 */
	public static void loadImg(String url, ImageView iv) {
		imageLoader.displayImage(url, iv, options);
	}
	
	private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();  

	/**
	 * 判断是否为今天(效率比较高)
	 * 
	 * @param day
	 *            传入的 时间 "2016-06-28 10:10:30" "2016-06-28" 都可以
	 * @return true今天 false不是
	 * @throws ParseException
	 * @throws java.text.ParseException 
	 */
	public static boolean IsToday(String day) throws  java.text.ParseException {

		Calendar pre = Calendar.getInstance();
		Date predate = new Date(System.currentTimeMillis());
		pre.setTime(predate);

		Calendar cal = Calendar.getInstance();
		Date date =getDateFormat().parse(day);
		cal.setTime(date);

		if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
			int diffDay = cal.get(Calendar.DAY_OF_YEAR)
					- pre.get(Calendar.DAY_OF_YEAR);

			if (diffDay == 0) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean IsTomorrow(String day) throws  java.text.ParseException {

		Calendar pre = Calendar.getInstance();
		Date predate = new Date(System.currentTimeMillis());
		pre.setTime(predate);

		Calendar cal = Calendar.getInstance();
		Date date = (Date) getDateFormat().parse(day);
		cal.setTime(date);

		if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
			int diffDay = cal.get(Calendar.DAY_OF_YEAR)
					- pre.get(Calendar.DAY_OF_YEAR);

			if (diffDay == 1) {
				return true;
			}
		}
		return false;
	}

	public static SimpleDateFormat getDateFormat() {
		if (null == DateLocal.get()) {
			DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
		}
		return DateLocal.get();
	}

}
